import mesh2vec.material as M

svgDPI = 90
mm2SVG = 90.0/25.4
inch2SVG = 90.0

def _pl2SVG(pl):
    #TODO: mesh2vec.svg.export._pl2SVG()
    p= pl[0]
    s= ""
    s += "M %.3f,%.3f "%(p.co.x,-p.co.y)
    for p in pl[1:]:
        s += "L %.3f,%3f "%(p.co.x, -p.co.y)
    s += "Z "
    return s

def poly(pl):
    svg=list()
    tmp= pl.copy()
    if self.attr & c.HOLE and self.order:
        tmp.reverse()
    elif not self.order:
        tmp.reverse()
        
    coeff= pl.props["light_coeff"] if pl.props.has_key("light_coeff") else 1.0

    s = '<path d="'
    s += _pl2SVG(tmp.pl)
    for hole in tmp.holes:
        s += _pl2SVG(hole.pl)
    s += '"'
    svg.append(s)
    if self.material:
        s = self.material.toSVGstyle(coeff)
        svg.append(s)
    svg.append('/>')
    return "\n".join(svg)


def drawing(dwg, filename):
    scale = dwg.scale
        
    #i need to convert to pixels, path data have no untis (stupid svg)
    if dwg.units == 'mm':
        scale *= mm2SVG
    elif dwg.units == 'cm':
        scale *= mm2SVG * 10
    elif dwg.units == 'm':
        scale *= mm2SVG * 1000
    elif dwg.units == 'km':
        scale *= mm2SVG * 1E6
    elif dwg.units == 'inch':
        scale *= inch2SVG
    
    svgWidth = dwg.width * scale
    svgHeight = dwg.height * scale

    if not self.layers:
        self.makeLayers()

    svg = ['<?xml version="1.0" encoding="iso-8859-1" standalone="no"?>',\
            '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">',\
            '<svg xmlns="http://www.w3.org/2000/svg" ',\
            'xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" ',\
            'width="%f" height="%f" >'% (svgWidth, svgHeight)]

    for layername,layer in self.layers:
        svg.append('<g id="%s" inkscape:groupmode="layer" inkscape:label="%s">'%(layername,layername))
        for pl in layer:
            svg_pl = pl.copy()
            svg_pl.flatten()
            svg_pl.scale(scaleF)
            #svg 0,0 is at top left corner
            svg_pl.move(svgWidth/2.0, -svgHeight/2.0 ,0.0)

            svg.append(poly(svg_pl))

        svg.append("</g>")
        
    svg.append("</svg>")
    svg = '\n'.join(svg)
    if file is None:
        return svg
    else:
        F = open(filename, 'w+')
        F.write(svg)
        F.close()

def material(mat, coeff=1.0):
    s='style="'
    fillC = [mat.fill_color[0]*coeff,\
             mat.fill_color[1]*coeff,\
             mat.fill_color[2]*coeff]
    fillC = M.col2RGB(fillC)
    fillC = "rgb(" + str(fillC[0]) + "," + str(fillC[1]) + "," + str(fillC[2]) + ")"
    s+= 'fill:'+ fillC +';'
    lineC = col2RGB(mat.line_color)
    lineC = "rgb(" + str(lineC[0]) + "," + str(lineC[1]) + "," + str(lineC[2]) + ")"
    s+= 'stroke:'+ lineC +';'
    lineW = str(mat.line_weight)
    s+=' stroke-width:'+ lineW +'mm;'
    if not mat.alpha is None:
        s+='fill-opacity:'+str(mat.alpha)+';'
    s+='stroke-linecap:round ;stroke-linejoin :round; stroke-miterlimit:4; fill-rule:evenodd; enable-background:accumulate"'
    return s
